package com.nf1000.balancelibrary;

import java.io.ByteArrayOutputStream;

/**
 * Created by Jason on 2016/3/22.
 */
public class StringUtils {
    public static byte[] hexStringToByteArray(String digits) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            for(int e = 0; e < digits.length(); e += 2) {
                char c1 = digits.charAt(e);
                if(e + 1 >= digits.length()) {
                    throw new IllegalArgumentException("hexUtil.odd");
                }

                char c2 = digits.charAt(e + 1);
                byte b = 0;
                byte b1;
                if(c1 >= 48 && c1 <= 57) {
                    b1 = (byte)(b + (c1 - 48) * 16);
                } else if(c1 >= 97 && c1 <= 102) {
                    b1 = (byte)(b + (c1 - 97 + 10) * 16);
                } else {
                    if(c1 < 65 || c1 > 70) {
                        throw new IllegalArgumentException("hexUtil.bad");
                    }

                    b1 = (byte)(b + (c1 - 65 + 10) * 16);
                }

                if(c2 >= 48 && c2 <= 57) {
                    b1 = (byte)(b1 + (c2 - 48));
                } else if(c2 >= 97 && c2 <= 102) {
                    b1 = (byte)(b1 + c2 - 97 + 10);
                } else {
                    if(c2 < 65 || c2 > 70) {
                        throw new IllegalArgumentException("hexUtil.bad");
                    }

                    b1 = (byte)(b1 + c2 - 65 + 10);
                }

                baos.write(b1);
            }
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }

        return baos.toByteArray();
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if(src != null && src.length > 0) {
            char[] buffer = new char[2];

            for(int i = 0; i < src.length; ++i) {
                buffer[0] = Character.forDigit(src[i] >>> 4 & 15, 16);
                buffer[1] = Character.forDigit(src[i] & 15, 16);
                stringBuilder.append(buffer);
            }

            return stringBuilder.toString().toUpperCase();
        } else {
            return null;
        }
    }

    public static String bytes2HexString(byte[] b) {
        String ret = "";

        for(int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if(hex.length() == 1) {
                hex = '0' + hex;
            }

            ret = ret + hex;
        }

        return ret;
    }


    public static String getBCC(byte[] data) {
        String ret = "";
        byte[] BCC = new byte[1];

        for(int hex = 0; hex < data.length; ++hex) {
            BCC[0] ^= data[hex];
        }

        String var4 = Integer.toHexString(BCC[0] & 255);
        if(var4.length() == 1) {
            var4 = '0' + var4;
        }

        ret = ret + var4.toUpperCase();
        return ret;
    }
}
